<?php
require_once '../../server/utils/functions.php';
require_once '../../server/model/User.php';
require_once '../../server/model/Crypt.php';
$db = db();
$crypt = new Crypt();
if(isset($_POST["USR_MAIL"])&& !empty($_POST["USR_MAIL"])){
    $parameters["USR_MAIL"] = utf8_decode(htmlentities($_POST["USR_MAIL"]));
}else{
    echo json_encode(Array("status"=>"error1"));
    exit(0);
}  
if(isset($_POST["USR_PASSWORD"])&& !empty($_POST["USR_PASSWORD"])){
    $parameters["USR_PASSWORD"] = $crypt->crypt(utf8_decode(htmlentities($_POST["USR_PASSWORD"])));
}else{
    echo json_encode(Array("status"=>"error1"));
    exit(0);
}
$req = $db->prepare("SELECT USR_ID FROM USER WHERE USR_MAIL=:USR_MAIL AND USR_PASSWORD=:USR_PASSWORD");
$req->execute($parameters);
$datas = $req->fetch();
if(isset($datas["USR_ID"])&& !empty($datas["USR_ID"])){
    echo json_encode(Array("status"=>"connected"));
    $user = new User($parameters["USR_MAIL"],$parameters["USR_PASSWORD"]);
    $user->setId($datas["USR_ID"]);
    $_SESSION["USER"]= $user;
}else{
    echo json_encode(Array("status"=>"error2"));
}
$req->closeCursor();
?>
